package ch07_GUI编程;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


	
public class EventListenerWithInnerClass {
	JLabel tip = new JLabel("请单击下面的按钮");
	JButton b1 = new JButton("确定");
	JButton b2 = new JButton("取消");

	public static void main(String[] args) {
		new EventListenerWithInnerClass().init();
	}

	void init() {
		BaseFrame f = new BaseFrame("使用内部类监听事件");
		f.setLayout(new FlowLayout());

		MyActionListener listener = new MyActionListener();
		b1.setActionCommand("ok");
		b2.setActionCommand("cancel");
		b1.addActionListener(listener);
		b2.addActionListener(listener);
		f.add(tip);
		f.add(b1);
		f.add(b2);
		
		f.showMe();
	}

	class MyActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			switch (e.getActionCommand() .toUpperCase() ) {
				case "OK":
					tip.setText("你单击了[" + b1.getText() + "]按钮");
					break;
				case "CANCEL":
					tip.setText("你单击了[" + b2.getText() + "]按钮");
					break;
				default :
					tip.setText("未知事件");
			}

		}

	}
}
